package com.starblink.android.basic.network.base

import com.blankj.utilcode.util.ToastUtils
import com.starblink.android.basic.base.BaseApplication
import com.starblink.android.basic.bridge.BridgeCommand
import com.starblink.android.basic.bridge.BridgeUtil
import com.starblink.android.basic.config.GuangConfig
import com.starblink.android.basic.ext.toJson
import com.starblink.android.basic.ext.toast
import com.starblink.android.basic.ext.toastUI
import com.starblink.android.basic.extension.navigation
import com.starblink.android.basic.sensorsdata.spm.impl.SpmTrackHandler
import com.starblink.android.basic.user.UserDataCenter
import com.starblink.android.basic.user.login.LoginManager
import com.starblink.android.basic.util.bus.FlowBus
import com.starblink.android.basic.util.bus.FlowConst
import com.starblink.basic.route.RoutePage
import com.starblink.basic.storage.SkStorage
import com.starblink.basic.style.R
import com.starblink.basic.util.ActivityStackManage
import com.starblink.basic.util.CommUtils
import com.starblink.basic.util.log.YYLogUtils
import com.starblink.rocketreserver.fragment.SiteVOF

object BaseSiteSwitch {


    var homeSiteDialog = false

    //首次安装请求站点接口
    const val START_UP_SITE_FIRST = "START_UP_SITE_FIRST_V2"
    const val SITE_OBJ = "SITE_OBJ_V2"

    var currentSite: SiteVOF? = null

    //
    fun startUpPageSwitchSite(site: SiteVOF) {

        //isPlay() 是生产环境  ||  canSwitchPub（） 是否可以切生产
        if (isPlay() || canSwitchPub()) {
//        if (true) {
            currentSite = site
            SkStorage.put(SITE_OBJ, site.toJson())
//            site.type 1为欧洲 2为美国
            if (site.type == 1) {
                switchSite(
                    BaseApiConfig.SITE_NOT_NOT_DEFAULT,
                    callback = {
                        handleNext()
                        //跨进程切换Web进程站点
                        BridgeUtil.sendCommonData(
                            BridgeCommand.Common.switchSite, BaseApiConfig.SITE_NOT_NOT_DEFAULT
                        )
                    })
            } else {
                switchSite(ApiSiteEnum.PUB.name,
                    callback = {
                        handleNext()
                        //跨进程切换Web进程站点
                        BridgeUtil.sendCommonData(
                            BridgeCommand.Common.switchSite, ApiSiteEnum.PUB.name
                        )
                    })
            }

        } else if (isCI()) {
            //如果是测试环境初始化 9527
            switchSite(ApiSiteEnum.PRE.name,
                callback = {
                    handleNext()
                })
            //跨进程切换Web进程站点
            BridgeUtil.sendCommonData(
                BridgeCommand.Common.switchSite, ApiSiteEnum.PRE.name
            )
        } else {
            handleNext()
        }

    }


    inline fun switchSite(sitStringUpCase: String, crossinline callback: () -> Unit) {
        SkStorage.put(START_UP_SITE_FIRST, false)
        if (BaseApiConfig.standardCfgSet[sitStringUpCase] != null) {
            BaseApiConfig.svrApiCfgEnv = BaseApiConfig.standardCfgSet[sitStringUpCase]!!
            SkStorage.put(GuangConfig.CURRENT_ENV_9527, sitStringUpCase)
            UserDataCenter.clearToken()
            callback.invoke()
        } else {
            ActivityStackManage.killAppAndRestart()
        }
    }


    /**
     * 是否是应用市场包
     */
    private fun isPlay(): Boolean {
        return BaseApplication.CURRENT_FLAVOR == BaseApiConfig.DEFAULT_FLAVOR
    }

    fun isCI(): Boolean {
        return BaseApplication.CURRENT_FLAVOR == BaseApiConfig.TEST_CI_FLAVOR
    }

    //启动页不自动切站点
    fun isQaUse(): Boolean {
        return SkStorage.getBoolean(GuangConfig.QA_NOT_SWITCH_SITE, false)
    }

    fun canSwitchPub(): Boolean {
        return SkStorage.getBoolean(GuangConfig.SWITCH_SITE_CI_CTL, false)
    }

    fun setQaUse(qaStatus: Boolean) {
        SkStorage.put(GuangConfig.QA_NOT_SWITCH_SITE, qaStatus)
    }

    fun setSwitchPub(s: Boolean) {
        SkStorage.put(GuangConfig.SWITCH_SITE_CI_CTL, s)
    }


    private fun handleNext() {
        handleSiteSwitchStatus(true)
        FlowBus.boolFlowBusNotify(FlowConst.LOGIN_STATUS_CHANGE)
        //登录状态 主进程向web进程发送通知
        BridgeUtil.sendFlowBus(FlowConst.LOGIN_STATUS_CHANGE)
        FlowBus.boolFlowBusNotify(FlowConst.HOME_GUIDE_NEXT)
    }


    private fun handleSiteSwitchStatus(hasChange: Boolean) {
        SpmTrackHandler.siteStatusHasSwitch = !hasChange
    }
}